<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="业主ID" prop="userId">
        <el-input
          v-model="queryParams.userId"
          placeholder="请输入业主ID"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <!-- 查询条件的投诉类型下拉框（已复用选项，无需改代码） -->
      <el-form-item label="投诉类型" prop="complaintStyle">
        <el-select
            v-model="queryParams.complaintStyle"
            placeholder="请选择投诉类型"
            style="width: 160px"
            clearable
            @keyup.enter="handleQuery"
        >
          <el-option
              v-for="item in complaintStyleOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['comComplaintTSSH:complaintSHYYY:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="complaintSHYYYList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
<!--      <el-table-column label="主键ID" align="center" prop="id" />-->
      <el-table-column label="业主ID" align="center" prop="userId" />
      <el-table-column label="投诉类型" align="center" prop="complaintStyle" />
      <el-table-column label="投诉内容" align="center" prop="complaintInfo" />
      <el-table-column label="投诉照片" align="center" prop="complaintPhoto" width="100">
        <template #default="scope">
          <image-preview :src="scope.row.complaintPhoto" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="投诉时间" align="center" prop="createTime">
        <template #default="scope">
          {{ formatDate(scope.row.createTime) }}
        </template>
      </el-table-column>
      <el-table-column label="投诉状态" align="center" prop="complaintStatus">
        <template #default="scope">
          <el-tag :type="getComplaintStatusTagType(scope.row.complaintStatus)">
            {{ getComplaintStatusText(scope.row.complaintStatus) }}
          </el-tag>
        </template>
      </el-table-column>
<!--      <el-table-column label="投诉状态" align="center" prop="complaintStatus" />-->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['comComplaintTSSH:complaintSHYYY:edit']">处理</el-button>
<!--          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['comComplaintTSSH:complaintSHYYY:remove']">删除</el-button>-->
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改投诉处理对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="complaintSHYYYRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item  label="业主ID" prop="userId">
          <el-input v-model="form.userId" disabled placeholder="请输入业主ID" />
        </el-form-item>
        <el-form-item label="投诉类型" prop="complaintStyle">
          <el-input v-model="form.complaintStyle" disabled placeholder="请输入投诉类型" />
        </el-form-item>
        <el-form-item label="投诉内容" prop="complaintInfo">
          <el-input v-model="form.complaintInfo" disabled type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="投诉照片"  prop="complaintPhoto">
          <image-upload disabled v-model="form.complaintPhoto"/>
        </el-form-item>
        <!-- 替换原有的投诉状态输入框，改为单选框 -->
        <el-form-item label="投诉状态" prop="complaintStatus">
          <el-radio-group v-model="form.complaintStatus" style="display: flex; gap: 15px;">
            <el-radio :label="0">待处理</el-radio>
            <el-radio :label="1">已处理</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="ComplaintSHYYY">
import { listComplaintSHYYY, getComplaintSHYYY, delComplaintSHYYY, addComplaintSHYYY, updateComplaintSHYYY } from "@/api/comComplaintTSSH/complaintSHYYY"

const { proxy } = getCurrentInstance()

const complaintSHYYYList = ref([])
const open = ref(false)
const loading = ref(true)
const showSearch = ref(true)
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const total = ref(0)
const title = ref("")

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    userId: null,
    complaintStyle: null,
    complaintInfo: null,
    complaintPhoto: null,
    complaintStatus: 0
  },
  rules: {
    userId: [
      { required: true, message: "业主ID不能为空", trigger: "blur" }
    ],
  }
})

const { queryParams, form, rules } = toRefs(data)

/** 查询投诉处理列表 */
function getList() {
  loading.value = true
  listComplaintSHYYY(queryParams.value).then(response => {
    complaintSHYYYList.value = response.rows
    total.value = response.total
    loading.value = false
  })
}

// 取消按钮
function cancel() {
  open.value = false
  reset()
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    userId: null,
    complaintStyle: null,
    complaintInfo: null,
    complaintPhoto: null,
    createTime: null,
    complaintStatus: null
  }
  proxy.resetForm("complaintSHYYYRef")
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef")
  handleQuery()
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id)
  single.value = selection.length != 1
  multiple.value = !selection.length
}

/** 新增按钮操作 */
function handleAdd() {
  reset()
  open.value = true
  title.value = "添加投诉处理"
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset()
  const _id = row.id || ids.value
  getComplaintSHYYY(_id).then(response => {
    form.value = response.data
    open.value = true
    title.value = "修改投诉处理"
  })
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["complaintSHYYYRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateComplaintSHYYY(form.value).then(response => {
          proxy.$modal.msgSuccess("处理成功")
          open.value = false
          getList()
        })
      } else {
        addComplaintSHYYY(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功")
          open.value = false
          getList()
        })
      }
    }
  })
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value
  proxy.$modal.confirm('是否确认删除投诉处理编号为"' + _ids + '"的数据项？').then(function() {
    return delComplaintSHYYY(_ids)
  }).then(() => {
    getList()
    proxy.$modal.msgSuccess("删除成功")
  }).catch(() => {})
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('comComplaintTSSH/complaintSHYYY/export', {
    ...queryParams.value
  }, `complaintSHYYY_${new Date().getTime()}.xlsx`)
}


const complaintStyleOptions = ref([
  { label: "保洁清洁问题（公共区域脏乱）", value: "保洁清洁问题" },
  { label: "安保管理问题（人员/车辆管控差）", value: "安保管理问题" },
  { label: "维修响应问题（水电/设施报修不处理）", value: "维修响应问题" },
  { label: "噪音扰民（装修/邻居/商业噪音）", value: "噪音扰民" },
  { label: "停车管理问题（车位被占/乱停车）", value: "停车管理问题" },
  { label: "费用问题（物业费/代收费用异议）", value: "费用问题" },
  { label: "邻里纠纷（占用公共空间/宠物扰民）", value: "邻里纠纷" },
  { label: "公共设施损坏（电梯/路灯/器材坏了）", value: "公共设施损坏" },
  { label: "其他问题", value: "其他问题" }
]);

// 【新增1】时间格式化函数：将 2025-09-18 19:28:11 转为 2025年9月18日
const formatDate = (dateString) => {
  if (!dateString) return ''; // 处理空时间的情况
  const date = new Date(dateString);
  const year = date.getFullYear();
  const month = date.getMonth() + 1; // 月份从0开始，需+1
  const day = date.getDate();
  return `${year}年${month}月${day}日`;
};

const getComplaintStatusText = (state) => {
  switch (state) {
    case 0: return '待处理';
    case 1: return '已处理';
    default: return '未知状态';
  }
};

const getComplaintStatusTagType = (state) => {
  switch (state) {
    case 1: return 'success';
    case 0: return 'danger';
    default: return 'warning';
  }
};

const complaintStatusOptions = ref([
  { label: "待处理", value: 0 },  // value需与数据库存储的状态值一致（数字类型）
  { label: "已处理", value: 1 }
]);
getList()
</script>
